package com.jims.master.common.chart;


import com.jims.master.common.utils.DateUtil;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.NumberUtil;
import com.jims.master.common.utils.TwdConstant;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.nurse.entity.*;
import com.jims.master.modules.sys.utils.DeptUtils;
import com.jims.master.modules.sys.utils.DictUtils;
import org.apache.commons.collections.CollectionUtils;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.*;
import java.util.List;

public class ChildTwdChartService {

    private double top_length = 2.7; // 顶端的高度
    private double top_rows = 9; // 顶部的行数
    private double top_row_height = top_length / (double) top_rows; // 顶部每行的高度

    private int bottom_length = 3; // 底部的高度
    private int bottom_rows = 10;// 底部的行数
    private double bottom_row_height = bottom_length / (double) bottom_rows; // 底部每行的高度

    private int grid_left = 6; // 左Y轴格子数
    private int grid_right = 0; // 右Y轴格子数

    private int x_unit = 6; // 多少个小格子组成一个大格子
    private double grid_row_height = 0.2; // 数据区每个格子的高度
    private double grid_col_width = 1.0;
    private int y_grid_length = 9; // y轴所有格子的长度
    private int x_grid_length = 42; // x轴所有格子的长度
    private int grid_rows = (int) Math.round(y_grid_length / grid_row_height); // 中间格子的行数

    private int start_temperature = 34; // 起始的体温 从底部高度开始算起

    private int x_length = grid_left + x_grid_length + grid_right; // x轴的长度
    private double y_length = top_length + y_grid_length + bottom_length; // y轴的长度

    Font font = new Font("SansSerif", Font.PLAIN, 12);
    private double round_size = 8d;

    public void initAllData(ChartProcessor chart, Map map) {
        initChart(chart);
        initLine(chart);
        initTopChart(chart, map);
        initGridChart(chart);
        initBottomChart(chart, map);
        initGridDynamicData(chart, map);

    }

    // 三测单 线条
    private void initLine(ChartProcessor chart) {
        // 四条边框
        // 下
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_frame_line_bottom", 0, 0.01);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_frame_line_bottom", x_length, 0.01);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_frame_line_bottom", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_frame_line_bottom", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_frame_line_bottom", ChartShape.LINE_NORMAL);
        // 左
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line", 0, 0);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line", 0, y_length - top_row_height * 4);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_y_line", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_y_line", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_y_line", ChartShape.LINE_WIDE);
        // 右
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line_right2", x_length - 0.0005, 0);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line_right2", x_length - 0.0005, y_length - top_row_height * 4);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_y_line_right2", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_y_line_right2", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_y_line_right2", ChartShape.LINE_WIDE);

        // 住院天数
        chart .addData(ChartProcessor.AXIS_LEFT, "basic_line_top", 0, bottom_length + y_grid_length + top_row_height
                * 5);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_line_top", x_length, bottom_length + y_grid_length
                + top_row_height * 5);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_line_top", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_line_top", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_line_top", ChartShape.LINE_NORMAL);

        // 左2, 左侧title与右侧内容的分界线
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line_sec", grid_left, 0);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_y_line_sec", grid_left, y_length - top_row_height * 4);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_y_line_sec", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_y_line_sec", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_y_line_sec", ChartShape.LINE_NORMAL);

        // 中部与底部的分界线
        chart.addData(ChartProcessor.AXIS_LEFT, "bottom_horizatal_line", 0, bottom_length + top_row_height);
        chart.addData(ChartProcessor.AXIS_LEFT, "bottom_horizatal_line", x_length - grid_right, bottom_length + top_row_height);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "bottom_horizatal_line", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "bottom_horizatal_line", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "bottom_horizatal_line", ChartShape.LINE_NORMAL);

        // 中部与顶部的分界线
        chart.addData(ChartProcessor.AXIS_LEFT, "top_horizatal_line", 0, y_length - top_length + top_row_height);
        chart.addData(ChartProcessor.AXIS_LEFT, "top_horizatal_line", x_length - grid_right, y_length - top_length + top_row_height);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "top_horizatal_line", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "top_horizatal_line", Color.BLACK);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "top_horizatal_line", ChartShape.LINE_NORMAL);


        // 纵向格子
        for (int i = 0; i <= x_grid_length; i++) {
            float chartShape = ChartShape.LINE_THIN;
            double startPoint = bottom_length + top_row_height;
            double endPoint = y_length - top_row_height * 7;
            if (i % 6 == 0) {
                chartShape = ChartShape.LINE_WIDE;
                startPoint = 0;
                endPoint += top_row_height * 3;
            }
            String line_name = "basic_line_ruliang_vertical_sec_" + i;
            chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + i * 0.92, startPoint);
            chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + i * 0.92, endPoint);
            chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, line_name, false);
            chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name, Color.BLACK);
            chart.setLineWidth(ChartProcessor.AXIS_LEFT, line_name, chartShape);
        }

        // 横向格子
        for (int i = 0; i < grid_rows; i++) {
            String line_name = "grid_horizontal_line" + i;
            float horizontalShape = ChartShape.LINE_THIN;
            Color horizontalColor = Color.BLACK;
            if (i % 5 == 0) {
                horizontalShape = ChartShape.LINE_WIDE;
            }
            if (i == 20) // 37度对应线为红色
            {
                horizontalColor = Color.RED;
            }
            chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left, bottom_length + grid_row_height * i + top_row_height);
            chart.addData(ChartProcessor.AXIS_LEFT, line_name, x_length - grid_right -3.5 , bottom_length + grid_row_height
                    * i + top_row_height);
            chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, line_name, false);
            chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name, horizontalColor);
            chart.setLineWidth(ChartProcessor.AXIS_LEFT, line_name, horizontalShape);
        }

        for(int i = 2; i < 5; i++) {
            chart.addData(ChartProcessor.AXIS_LEFT, "basic_line_top" + i, 0, bottom_length + y_grid_length
                    + top_row_height * i);
            chart.addData(ChartProcessor.AXIS_LEFT, "basic_line_top" + i, x_length - grid_right, bottom_length
                    + y_grid_length + top_row_height * i);
            chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_line_top" + i, false);
            chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_line_top" + i, Color.BLACK);
            chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_line_top" + i, ChartShape.LINE_THIN);
        }
        //底部横线
        for(int i = 1; i < bottom_rows; i++) {
            chart
                    .addData(ChartProcessor.AXIS_LEFT, "basic_line_bottom_" + i, 0, bottom_length - bottom_row_height
                            * i);
            chart.addData(ChartProcessor.AXIS_LEFT, "basic_line_bottom_" + i, x_length - grid_right, bottom_length
                    - bottom_row_height * i);
            chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_line_bottom_" + i, false);
            chart.setLineColor(ChartProcessor.AXIS_LEFT, "basic_line_bottom_" + i, Color.BLACK);
            chart.setLineWidth(ChartProcessor.AXIS_LEFT, "basic_line_bottom_" + i, ChartShape.LINE_THIN);
        }

    }

    /**
     * 初始化坐标轴、边框
     * @param chart
     */
    private void initChart(ChartProcessor chart) {
        // 初始化各坐标轴
        chart.setXRange(-1, x_length + 1);
        chart.setXUnit(1);
        chart.setXVisible(false);
        chart.setBgHorizontalLineColor(Color.white);
        chart.setBgVerticalLineColor(Color.white);

        chart.setYUnit(ChartProcessor.AXIS_LEFT, 1);
        chart.setYRange(ChartProcessor.AXIS_LEFT, -0.3, y_length);
        chart.addData(ChartProcessor.AXIS_LEFT, "basic_left", 0, y_length);
        chart.setLineVisible(ChartProcessor.AXIS_LEFT, "basic_left", false);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "basic_left", false);
        chart.setYVisible(ChartProcessor.AXIS_LEFT, false);


    }

    /**
     * **
     * 顶部标题初始化
     */
    private void initTopChart(ChartProcessor chart, Map map) {
        PatVisit pv = (PatVisit) map.get("jibenMap");
        NewbornRec newborn = (NewbornRec) map.get("newborn");
        chart.addBackgroundValue("姓 名：", grid_left / 3, bottom_length + y_grid_length + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue(pv.getPatVisitIndex().getName(), grid_left / 2 + x_unit / 2 - 1.5, bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);
        chart.addBackgroundValue("性 别：", grid_left / 2 + x_unit / 3 + x_unit, bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue(DictUtils.getDictLabel(pv.getPatVisitIndex().getSex(), "SEX_DICT",""), grid_left / 2 + x_unit * 2 - 2.5, bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue("第", grid_left / 2 + x_unit / 3 + x_unit * 2 - 1, bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue(newborn.getParityNo(), grid_left / 2 + x_unit * 2 + 2.5 , bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue("胎", grid_left / 2 + x_unit * 2 + 4 , bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue("科 室：", grid_left / 2 + x_unit * 3 + 2 , bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);
        chart.addBackgroundValue(DeptUtils.getDeptName(pv.getDeptStayed(), ""), grid_left / 2 + x_unit * 3 + x_unit -1, bottom_length + y_grid_length
                + top_row_height * 5 + 0.1, font);
        chart.addBackgroundValue("床 号：", x_length - x_unit * 2 - 3, bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);
        chart.addBackgroundValue(pv.getBedNo(), x_length - x_unit * 2 - 0.5, bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);
        chart.addBackgroundValue("住院号：", x_length - x_unit - 2, bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);
        chart.addBackgroundValue(pv.getVisitNo(), x_length - x_unit / 2 - 1, bottom_length + y_grid_length + top_row_height * 5
                + 0.1, font);


        chart.addBackgroundValue("体温", x_length - x_unit * 2 + 3, bottom_length + y_grid_length + top_row_height * 6 + 0.2,
                font);
        chart.addData(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw1", x_length - x_unit * 2 + 4 + 0.2, (bottom_length + y_grid_length + top_row_height * 6) + 0.25);
        chart.addData(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw2", x_length - x_unit * 2 + 4 + 0.2, (bottom_length + y_grid_length + top_row_height * 6) + 0.25);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw1", Color.BLUE);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw2", Color.BLUE);
        chart.setLineShape(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw1", ChartShape.getLine(
                ChartShape.LINE, new Point2D.Double(-3, -3), new Point2D.Double(3, 3))); // 叉
        chart.setLineShape(ChartProcessor.AXIS_LEFT, "grid_point_temperatures_kw2", ChartShape.getLine(
                ChartShape.LINE, new Point2D.Double(-3, 3), new Point2D.Double(3, -3)));

        chart.addBackgroundValue("体重", x_length - x_unit + 2, bottom_length + y_grid_length + top_row_height * 6 + 0.2,
                font);
        chart.addData(ChartProcessor.AXIS_LEFT, "grid_point_heartRate_1_1", x_length - x_unit / 3 - 0.7, (bottom_length + y_grid_length + top_row_height * 6) + 0.25);
        chart.setLineShape(ChartProcessor.AXIS_LEFT, "grid_point_heartRate_1_1", ChartShape.getShape(
                ChartShape.ROUND, round_size, round_size));
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "grid_point_heartRate_1_1", Color.RED);


        chart.addBackgroundValue("新 生 儿 体 温 记 录 单", x_length / 2, bottom_length + y_grid_length + top_row_height * 6 + 0.1,
                new Font("SansSerif", Font.BOLD, 25));
        chart.addBackgroundValue("承德市双滦区人民医院（承德市精神病医院）", x_length / 2, bottom_length + y_grid_length + top_row_height * 8,
                new Font("SansSerif", Font.PLAIN, 15));

        /*****
         * 顶部数据初始化
         **/
        chart.addBackgroundValue("日           期", grid_left / 2, bottom_length + y_grid_length + top_row_height * 4 + 0.1,
                font);
        chart.addBackgroundValue("出  生  日  数", grid_left / 2, bottom_length + y_grid_length + top_row_height * 3 + 0.1,
                font);
        chart.addBackgroundValue("发  病  日  数", grid_left / 2, bottom_length + y_grid_length + top_row_height * 2
                + 0.1, font);

        // 时间节点2 ,6,10,14,18,22
        for (int i = 0; i < x_grid_length; i++) {
            Integer[] hours = TwdConstant.HOURS;
            int beginHour = hours[i < x_unit ? i : i % x_unit];
            Color hourColor = Color.BLACK;
            chart.addBackgroundValue(beginHour + "", grid_left + i*0.92 + 0.4, bottom_length + y_grid_length + 0.1 + top_row_height, font,
                    hourColor);
        }
        chart.addBackgroundValue("时            间", grid_left / 2, bottom_length + y_grid_length + 0.1 + top_row_height, font, Color.BLACK);
        chart.addBackgroundValue("体重", grid_left + x_grid_length - 1.5, bottom_length + y_grid_length + 0.1 + top_row_height, font, Color.BLACK);

        /**
         * 顶部动态数据展示
         * 日期信息显示 例子：2012-07-31、08-01、02、03、04、05
         */
        List<String> twdDateList = (List<String>) map.get("twdDateList");

        if (twdDateList != null && twdDateList.size() > 0) {
            for (int i = 0; i < twdDateList.size(); i++) {
                //日期
                double x = grid_left * (i*0.92 + 1) + x_unit / 2;
                chart.addBackgroundValue(twdDateList.get(i), x, bottom_length
                        + y_grid_length + top_row_height * 4 + 0.1, font); // 日期
                long diffDays = DateUtil.getDateDiff(DateUtil.parseDate(twdDateList.get(i)), newborn.getDateOfBirth());
                // 出生日数
                chart.addBackgroundValue(diffDays == 0 ? "出生日" : diffDays + "", x, bottom_length + y_grid_length
                        + top_row_height * 3 + 0.1, font);

                String info = "/";
                if(pv.getSituationDate() != null) {
                    long situationDays = DateUtil.getDateDiff(DateUtil.parseDate(twdDateList.get(i)), pv.getSituationDate());
                    if(situationDays == 0) {
                        info = "发病日";
                    } else if(situationDays > 0){
                        info = situationDays + "";
                    }
                }
                // 发病日数
                chart.addBackgroundValue(info, x, bottom_length + y_grid_length + top_row_height * 2 + 0.1, font);

            }
        }
    }

    /**
     * 中间格子初始化
     */
    private void initGridChart(ChartProcessor chart) {
        // 初始化脉搏、体温坐标轴
        Font font = new Font("SansSerif", Font.PLAIN, 12);
        for (int i = 0; i < y_grid_length; i++) {
            String value_left = (start_temperature + i) + "℃";
            double y = bottom_length + i + ((i == y_grid_length-1) ? 0.8 : 0.92) + top_row_height;
            chart.addBackgroundValue(value_left, grid_left / 6 * 5 - 0.7, y, font, Color.BLUE);
        }
        int[] weights = {600, 400, 200, 3700, 200, 400, 600, 800, 1000};
        for (int i = 0; i < weights.length; i++) {
            double y = bottom_length + i + ((i == y_grid_length-1) ? 0.8 : 0.92) + top_row_height;
            chart.addBackgroundValue(weights[i] + "克", grid_left + x_grid_length - 1.5, y, font, Color.RED);
        }
        chart.addBackgroundValue("↑", grid_left + x_grid_length - 1.5, bottom_length + 4 + top_row_height + 0.45, font, Color.RED);
        chart.addBackgroundValue("出生体重", grid_left + x_grid_length - 1.5, bottom_length + 4 + top_row_height + 0.15, font, Color.RED);
        chart.addBackgroundValue("↓", grid_left + x_grid_length - 1.5, bottom_length + 4 + top_row_height - 0.35, font, Color.RED);

        chart.addBackgroundValue("体", grid_left / 4 + 0.3, bottom_length + y_grid_length, font,Color.BLUE);
        chart.addBackgroundValue("温", grid_left / 4 + 0.3, bottom_length + y_grid_length - 0.3, font,Color.BLUE);
        chart.addBackgroundValue("℃", grid_left / 4 + 0.3, bottom_length + y_grid_length - 0.6, font,Color.BLUE);
    }

    /**
     * 底部数据初始化
     */
    public void initBottomChart(ChartProcessor chart, Map map) {
        List<List<String>> itemInfos = (List<List<String>>) map.get("itemInfos");
        List<String> itemInfosTitles = (List<String>) map.get("itemInfosTitle");
        List<String> names = (List<String>) map.get("names");
        if(itemInfosTitles != null && itemInfosTitles.size() > 0) {
            int index = 0;
            boolean renderPluse = false;
            double[][] height = {{0.2},{0.1, 0.3}, {0, 0.2, 0.4}};
            for(String title : itemInfosTitles) {
                if(title.indexOf("血压") > -1) {
                    renderPluse = true;
                    chart.addBackgroundValue(title, grid_left / 2, bottom_length - bottom_row_height + 0.25, font);
                    for(int i = 0; i < itemInfos.get(index).size(); i++) {
                        String[] strs = itemInfos.get(index).get(i) == null ? null : itemInfos.get(index).get(i).split(" ");
                        if(strs != null) {
                            String s = "";
                            List<String> list = new ArrayList<>();
                            for(int j = 0; j < strs.length; j++) {
                                if(j %2 == 0) {
                                    s = strs[j];
                                } else {
                                    s += " " + strs[j];
                                    list.add(s);
                                }
                            }
                            if(strs.length % 2 == 1) {
                                if(strs.length > 2) {
                                    s += "　　　";
                                }
                                list.add(s);
                            }
                            Collections.reverse(list); // 倒序排列
                            for(int k = 0, len = list.size(); k < len; k++) {
                                chart.addBackgroundValue(list.get(k), grid_left * (i*0.92+1)-0.2 + x_unit / 2, bottom_length - bottom_row_height + height[len-1][k], font);
                            }
                        }
                    }
                } else {
                    int t = renderPluse ? index : index + 1;
                    chart.addBackgroundValue(title, grid_left / 2, bottom_length - bottom_row_height * (t+1) + 0.1, font);
                    for(int i = 0; i < itemInfos.get(index).size(); i++) {
                        chart.addBackgroundValue(itemInfos.get(index).get(i) == null ? "" : itemInfos.get(index).get(i),
                                grid_left * (i*0.92+1) + x_unit / 2, bottom_length - bottom_row_height * (t+1) + 0.1, font);
                    }
                }
                index ++;
            }
        }
        chart.addBackgroundValue("签名", grid_left / 2, bottom_length - bottom_row_height * 10 + 0.1, font);
        for(int i = 0;  i < names.size(); i++) {
            chart.addBackgroundValue(names.get(i) == null ? "" : names.get(i),
                    grid_left * (i*0.92+1) + x_unit / 2, bottom_length - bottom_row_height * 10 + 0.1, font);
        }

        chart.addBackgroundValue(map.get("pageInfo").toString(), x_length / 2 , -0.2, font);

    }

    /**
     * 动态格子数据的初始化
     *
     * @param chart
     * @param map
     */
    public void initGridDynamicData(ChartProcessor chart, Map map) {
        // begin
        List<String> twdDateList = (List<String>) map.get("twdDateList"); // 日期集合
        List<NurseTempInfo> tempInfoList = (List<NurseTempInfo>) map.get("tempInfoList"); // 体温单集合
        Integer[] hours = TwdConstant.HOURS;

        List<NurseVitalSignsRec> recs = (List<NurseVitalSignsRec>) map.get("nurseEvent");
        // 护理提示内容
        if(recs != null && recs.size() > 0) {
            for(NurseVitalSignsRec rec : recs) {
                String admissionDate = DateUtils.formatDate(rec.getTimePoint());
                String inTime = rec.getVitalSigns() + DateUtils.formatToChineseDate(rec.getTimePoint());
                double y = bottom_length + grid_col_width * 9 + 0.13;
                for (int i = 0; i < twdDateList.size(); i++) {
                    if(twdDateList.get(i).equals(admissionDate)) {
                        int hour = Integer.parseInt(DateUtils.formatDate(rec.getTimePoint(), "HH"));
                        for (int j = 0; j < hours.length; j++) {
                            if(hour < hours[j]) {
                                if("转入".equals(rec.getVitalSigns().trim())) {
                                    continue;
                                }
                                for(int k = 0; k < inTime.length(); k++) {
                                    chart.addBackgroundValue(inTime.charAt(k) + "", grid_left + i * 6 + grid_col_width / 2 + j * 0.92, y - k * grid_row_height, font);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }




        if (CollectionUtils.isEmpty(tempInfoList)) {
            return;
        }
        for (int i = 0; i < twdDateList.size(); i++) {
            NurseTempInfo resultInfo = null;
            for (NurseTempInfo tempInfo : tempInfoList) {
                if (tempInfo.getInspecttionDate() != null) {
                    if (twdDateList.get(i).equals(DateUtil.formatDate(tempInfo.getInspecttionDate()))) {
                        resultInfo = tempInfo;
                        break;
                    }
                }
            }
            if (resultInfo != null) {
                List<NurseTempDetailInfo> detailInfoList = new ArrayList<NurseTempDetailInfo>();
                detailInfoList.addAll(resultInfo.getNurseTempDetailInfos());
                int k = 0;

                for (int j = 0; j < hours.length; j++) // 遍历每天的数据
                {
                    NurseTempDetailInfo resultDetailInfo = null;
                    for (NurseTempDetailInfo detailInfo : detailInfoList) {
                        if (hours[j].intValue() == detailInfo.getTimePoint()) {
                            resultDetailInfo = detailInfo;
                            if (detailInfo.getBreathe() != null)
                                k++;
                            break;
                        }
                    }
                    if (resultDetailInfo != null) {

                        double grid_temperature_height = 0; // 体温单对应的y轴值
                        double grid_pulse_height = 0; // 脉搏对应的y轴值
                        double grid_heartRate_height = 0; // 心率对应的y轴值
                        boolean temperature_pulse = false; // 体温、脉搏重合
                        boolean temperature_heartRate = false; // 体温、心率重合
                        if (resultDetailInfo.getTemperature() != null)
                            grid_temperature_height = NumberUtil.round(Double.valueOf(resultDetailInfo.getTemperature() - (start_temperature - bottom_length)));
                        if (resultDetailInfo.getPulse() != null)
                            grid_pulse_height = NumberUtil.round(bottom_length + (resultDetailInfo.getPulse() - 20)
                                    * 0.05);
                        if (resultDetailInfo.getHeartRate() != null)
                            grid_heartRate_height = NumberUtil.round(bottom_length
                                    + (resultDetailInfo.getHeartRate() - 20) * 0.05);
                        if (Math.abs(NumberUtil.sub(grid_temperature_height, grid_pulse_height)) < 0.05) // 体温

                        if (Math.abs(NumberUtil.sub(grid_temperature_height, grid_heartRate_height)) < 0.05) // 体温心率重合
                        {
                            temperature_heartRate = true;
                        }

                        /**
                         * 体温
                         */
                        if (resultDetailInfo.getTemperature() != null ) {
                            grid_temperature_height = NumberUtil.round(Double.valueOf(resultDetailInfo.getTemperature())) - start_temperature + bottom_length + 0.3 + grid_col_width;;
                            // 画点
                            String line_name = "grid_point_temperatures_" + i + "_" + j;
                            String line_name1 = line_name + "1";
                            String line_name2 = line_name + "2";
                            if (temperature_pulse || temperature_heartRate) {
                                line_name1 = "g_grid";
                                line_name2 = "f_grid";
                            }
                            chart.addData(ChartProcessor.AXIS_LEFT, line_name1, grid_left + i * 6 * 0.92 + grid_col_width
                                    / 2 + j * 0.92, grid_temperature_height);
                            chart.addData(ChartProcessor.AXIS_LEFT, line_name2, grid_left + i * 6 * 0.92 + grid_col_width
                                    / 2 + j * 0.92, grid_temperature_height);
                            chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name1, Color.BLUE);
                            chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name2, Color.BLUE);
                            chart.setLineShape(ChartProcessor.AXIS_LEFT, line_name1, ChartShape.getLine(
                                    ChartShape.LINE, new Point2D.Double(-3, -3), new Point2D.Double(3, 3))); // 叉
                            chart.setLineShape(ChartProcessor.AXIS_LEFT, line_name2, ChartShape.getLine(
                                    ChartShape.LINE, new Point2D.Double(-3, 3), new Point2D.Double(3, -3)));

                            if (resultDetailInfo.getReductionTemperature() != null) // 降温后体温
                            {
                                double reducationTemperature = NumberUtil.round(Double.valueOf(resultDetailInfo.getReductionTemperature() - (start_temperature - bottom_length))) + 0.3 + grid_col_width;;
                                // 画圈
                                line_name = "grid_point_reductionTemperature_" + i + "_" + j;
                                chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + i * 6 * 0.92 + grid_col_width
                                        / 2 + j * 0.92, reducationTemperature);
                                chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, line_name, true);
                                chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name, Color.RED);
                                chart.setLineShape(ChartProcessor.AXIS_LEFT, line_name, ChartShape.getShape(
                                        ChartShape.ROUND, round_size + 3, round_size + 3));
                                chart.setLineVisible(ChartProcessor.AXIS_LEFT, line_name, false);
                                chart.setLineShapeFilledColor(ChartProcessor.AXIS_LEFT, line_name, Color.WHITE);

                                line_name = "grid_line_reductionTemperature_" + i + "_" + j;
                                // 画虚线
                                chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + i * 6 * 0.92 + grid_col_width
                                        / 2 + j * 0.92, grid_temperature_height);
                                chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + i * 6 * 0.92 + grid_col_width
                                        / 2 + j * 0.92, reducationTemperature);
                                chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, line_name, false);
                                chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name, Color.RED);
                                chart.setLineWidth(ChartProcessor.AXIS_LEFT, line_name, ChartShape.LINE_WIDE);
                                chart.setLineDash(ChartProcessor.AXIS_LEFT, line_name, true);
                                chart.setLineVisible(ChartProcessor.AXIS_LEFT, line_name, true);
                            }

                            // 画线
                            chart.addData(ChartProcessor.AXIS_LEFT, "grid_line_temperature", grid_left + i * 6 * 0.92
                                    + grid_col_width / 2 + j * 0.92, grid_temperature_height);
                            chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "grid_line_temperature", false);
                            chart.setLineColor(ChartProcessor.AXIS_LEFT, "grid_line_temperature", Color.BLUE);
                            chart.setLineWidth(ChartProcessor.AXIS_LEFT, "grid_line_temperature",
                                    ChartShape.LINE_THIN);
                            chart.setLineVisible(ChartProcessor.AXIS_LEFT, "grid_line_temperature", true);
                        }

                    }
                }
            }
        }

        List<NurseTempItemInfo> itemInfos = (List<NurseTempItemInfo>)map.get("weight");
        if(itemInfos != null && itemInfos.size() > 0) {
            for(int i = 0; i < itemInfos.size(); i++) {
                if(itemInfos.get(i).getItemValue() != null) {
                    double weight = Double.parseDouble(itemInfos.get(i).getItemValue());
                    if("KG".equals(itemInfos.get(i).getItemDict().getUnit().toUpperCase())) {
                        weight = weight * 1000;
                    }
                    addWeight(chart, weight, i, Integer.parseInt(DateUtils.formatDate(itemInfos.get(i).getUpdateDate(), "HH")) / 4);
                }
            }
        }
    }

    private void addWeight(ChartProcessor chart, double weight, int dateIndex, int intHourIndex) {
        double x = intHourIndex - 6 * (dateIndex + 1) * 0.08 + 0.1;
        double grid_weight_height = (weight - 3300) * 0.005 + bottom_length + 0.3 + grid_col_width * 2;
        // 画点
        String line_name = "grid_point_pulse_" + dateIndex + "_" + intHourIndex;
        chart.addData(ChartProcessor.AXIS_LEFT, line_name, grid_left + dateIndex * 6 + grid_col_width / 2
                + x, grid_weight_height);

        chart.setLineShape(ChartProcessor.AXIS_LEFT, line_name, ChartShape.getShape(
                ChartShape.ROUND, round_size, round_size));
        chart.setLineColor(ChartProcessor.AXIS_LEFT, line_name, Color.RED);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, line_name, true);
        chart.setLineVisible(ChartProcessor.AXIS_LEFT, line_name, false);

        // 画线
        chart.addData(ChartProcessor.AXIS_LEFT, "grid_line_pulse", grid_left + dateIndex * 6
                + grid_col_width / 2 + x, grid_weight_height);
        chart.setLineShapeVisible(ChartProcessor.AXIS_LEFT, "grid_line_pulse", false);
        chart.setLineColor(ChartProcessor.AXIS_LEFT, "grid_line_pulse", Color.RED);
        chart.setLineWidth(ChartProcessor.AXIS_LEFT, "grid_line_pulse", ChartShape.LINE_THIN);
        chart.setLineVisible(ChartProcessor.AXIS_LEFT, "grid_line_pulse", true);
    }

}
